package com.lonely.fx.tree_view;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

/**
 * @date: 2022-08-03 14:30
 * @author: ztkj-hzb
 * @description: treeview 基础属性设置
 **/
public class TreeViewWithBaseSettingMain extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {


        AnchorPane anchorPane = new AnchorPane();

        Button button = new Button("点击展开");

        //创建TreeView
        TreeView<String> treeView = new TreeView<>();

        //准备数据

        //创建根节点
        TreeItem<String> root = new TreeItem<>("中国");

        TreeItem<String> hb = new TreeItem<>("湖北省");
        TreeItem<String> hb_1 = new TreeItem<>("鄂州市");
        TreeItem<String> hb_2 = new TreeItem<>("武汉市");
        TreeItem<String> hb_3 = new TreeItem<>("襄樊市");
        TreeItem<String> hb_4 = new TreeItem<>("黄冈市");

        //添加子节点
        hb.getChildren().addAll(hb_1, hb_2, hb_3, hb_4);

        TreeItem<String> gd = new TreeItem<>("广东省");
        TreeItem<String> gd_1 = new TreeItem<>("广州市");
        TreeItem<String> gd_2 = new TreeItem<>("深圳市");
        TreeItem<String> gd_3 = new TreeItem<>("东莞市");
        gd.getChildren().addAll(gd_1, gd_2, gd_3);

        TreeItem<String> hn = new TreeItem<>("湖南省");
        TreeItem<String> hn_1 = new TreeItem<>("长沙市");
        TreeItem<String> hn_2 = new TreeItem<>("岳阳市");
        hn.getChildren().addAll(hn_1, hn_2);

        root.getChildren().addAll(hb, gd, hn);

        //设置根节点
        treeView.setRoot(root);

        //设置宽高
        treeView.setPrefSize(300, 300);

        //是否显示root节点,默认是true：显示
        //treeView.setShowRoot(false);


        //设置treeview显示样式
        /*treeView.setCellFactory(new Callback<TreeView<String>, TreeCell<String>>() {
            @Override
            public TreeCell<String> call(TreeView<String> param) {
                return new TreeCell<String>(){
                    *//**
         *
         *//*
                    @Override
                    protected void updateItem(String item, boolean empty) {
                        super.updateItem(item, empty);

                        if(empty){
                            return;
                        }

                        this.setGraphic(new Button(item));
                    }
                };
            }
        });*/

        //设置每个单元格大小
        //treeView.setFixedCellSize(40);

        //TODO 设置子节点特征

        //设置graphic
        root.setGraphic(new ImageView(new Image("icons/lbxx.png", 20, 20, true, true)));

        //设置是否展开子节点列表
        root.setExpanded(true);

        //设置节点值
        root.setValue("大中华");

        //判断指定节点是否是叶子节点, true:叶子节点
        System.out.println(root.isLeaf());

        //获取指定节点的父节点
        TreeItem<String> parent = hb.getParent();
        System.out.println(parent.getValue());

        //选择指定下标节点---注意这是以显示的层级的索引来计算，
        //hb.setExpanded(true);
        //treeView.getSelectionModel().select(1);
        //选择指定treeitem
        //treeView.getSelectionModel().select(hb_2);
        //选择第一个
        //treeView.getSelectionModel().selectFirst();
        //选择最后一个
        //treeView.getSelectionModel().selectLast();
        //选择当前选中的下一个
        //treeView.getSelectionModel().selectNext();
        //选择当前选中的上一个
        //treeView.getSelectionModel().selectPrevious();


        //TODO 设置选择默认是为多选
        treeView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
        //treeView.getSelectionModel().selectRange(0,3);
        //全选--可见tablecell全选
        //treeView.getSelectionModel().selectAll();

        //System.out.println("当前选中的cell数量：" + treeView.getSelectionModel().getSelectedItems().size());


        anchorPane.getChildren().addAll(button, treeView);
        AnchorPane.setTopAnchor(treeView, 100.0);
        primaryStage.setScene(new Scene(anchorPane));
        primaryStage.setWidth(600);
        primaryStage.setHeight(500);
        primaryStage.show();


        //是否展开

        button.setOnAction(new EventHandler<ActionEvent>() {
            boolean isExpand = false;

            @Override
            public void handle(ActionEvent event) {
                if (isExpand) {
                    //当前是展开状态， 需要收缩起来
                    expandOrShrink(root, false);
                    button.setText("点击展开");
                } else {
                    //当前是收缩状态，需要展开
                    expandOrShrink(root, true);
                    button.setText("点击缩起");

                }
                isExpand = !isExpand;
            }
        });


    }


    private void expandOrShrink(TreeItem<String> treeItem, boolean isExpand) {

        //如果是叶子节点，直接返回
        if (treeItem.isLeaf()) {
            return;
        }

        //非叶子节点,设置状态
        treeItem.setExpanded(isExpand);

        //获取下级节点
        for (TreeItem<String> child : treeItem.getChildren()) {
            expandOrShrink(child, isExpand);
        }

    }
}
